package com.igeek.health.job;

import cn.hutool.core.collection.CollectionUtil;
import com.igeek.health.constant.RedisConstant;
import com.igeek.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @Author fengqq
 * @Description 定时任务内容
 * @Date 2024/9/10  10:57
 */
@Slf4j
@Component
public class CleanImgJob extends QuartzJobBean {

    @Autowired(required = false)
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("-----------开始清理垃圾资源------------");
        //1.计算差值集合
        Set<Object> imgs =
                redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC, RedisConstant.SETMEAL_DB_PIC);
        if(CollectionUtil.isNotEmpty(imgs)){
            //2.遍历差集中的元素 进行删除七牛云的图片文件
            imgs.forEach(img->{
                log.info("-----------开始清理图片{}------------",img);
                boolean flag = QiniuUtils.deleteFromQiniu(img.toString());
                if(flag){
                    //3.七牛云的图片 删除成功后 删除redis中的存储的图片路径
                    redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC,img);
                }
            });
        }

    }
}
